Graphic = {
    dependencies = {
        Renderer
    }
}
---@diagnostic disable-next-line: duplicate-set-field
function Graphic:ctor()
    self._assembler = AssemblerGraphic.new(self)
    self._graphics = {}
    self.lineWidth = 1
    self.material = Material.default:clone()
end

function Graphic:beginPaint(r, g, b, a)
    self._canvas = GetTempCanvasN2(self.width, self.height)
    love.graphics.setCanvas(self._canvas)
    if r then
        love.graphics.clear(0, 0, 0, 0)
    else
        love.graphics.clear(r, g, b, a)
    end
end

function Graphic:curve(segments, calculator)
    local points = {}
    for i = 0, segments do
        local x, y = calculator(i / segments)
        table.insert(points, x)
        table.insert(points, y)
    end
    love.graphics.setBlendMode("replace", "premultiplied")
    love.graphics.setLineWidth(self.lineWidth)
    love.graphics.line(points)
    love.graphics.setBlendMode("alpha")

end

function Graphic:rectangle(x, y, w, h)
    love.graphics.rectangle("fill", x, y, w, h)
end

function Graphic:polygon(points)
    love.graphics.setBlendMode("replace", "premultiplied")
    love.graphics.polygon("fill", points)
    love.graphics.setBlendMode("alpha")
end

function Graphic:label(string, fontSize, fontName)
    local font = love.graphics.newFont(fontName or "core/resources/fonts/1_3.ttf", fontSize or 20)
    local text = love.graphics.newText(font, string)
    love.graphics.draw(text)
end

function Graphic:endPaint()
    love.graphics.setCanvas()
    local imageData = self._canvas:newImageData()
    self.material:setTexture(love.graphics.newImage(imageData))
    self.renderInfoDirty = true
end

function Graphic:curveHorizontal(p1, p2, segments)
    self:curve(segments, function (progress)
        return (p1.x + (p2.x - p1.x) * progress), (p1.y + (p2.y - p1.y) * EasingMethods.inOutCubic(progress))
    end)
end

function Node.Graphic()
    local node = Node.new()
    node:addComponent(Graphic)
    return node
end